package com.sofwin.controller;

import com.sofwin.common.ResponseData;
import com.sofwin.pojo.SysDept;
import com.sofwin.system.DeptService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @packageName: com.sofwin.controller
 * @user: andyliu
 * @date: 2021/9/23 11:34
 * @email 115176513@qq.com
 * @description: TODO
 */
@Controller
@RequestMapping("/dept")
public class DeptController {
    @Reference
    public DeptService deptService;

    /**
     * 跳转到部门列表页
     * @return
     */
    @GetMapping
    public String toData(){

        return "dept/data";
    }

    /**
     * 树状表格
     * @param dept
     * @return
     */
    @PostMapping("data")
    @ResponseBody
    public ResponseData data(SysDept dept){
        List<SysDept> depts = deptService.queryDepts(dept);
        return new ResponseData(200,depts);
    }

    @GetMapping("/toEdit")
    public String toEdit(SysDept dept, Model model){
        if(dept.getDeptId()!=null){
            // 根据id查询部门信息，并放入到作用域
            dept = deptService.queryById(dept.getDeptId());
            model.addAttribute("dept",dept);
        }
        return "dept/edit";
    }
    @PostMapping("/save")
    @ResponseBody
    public ResponseData save(SysDept dept){
        boolean status = deptService.saveOrUpdate(dept);
        if(status)return ResponseData.success("保存成功");
        return ResponseData.error();
    }

    @DeleteMapping("/deletes")
    @ResponseBody
    public ResponseData delete(Integer[] ids){
        boolean status = deptService.removeDepts(ids);
        if(status)return ResponseData.success("删除成功");
        return ResponseData.error();
    }



}
